//
//  QMyIncomeVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/16.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QMyIncomeVC.h"
#import "QIncomeCell.h"
#import "QLevelCell.h"
#import "QIncomePhoneCell.h"
#import "QMySuperiorVC.h"
#import "QMyCustomerVC.h"
#import "QOrderRecordVC.h"
//#import "TiXianWebVC.h"
#import "BindingCardVC.h"
#import "CashVC.h"
#import "MyCardVC.h"
#import "BingdingVC.h"
@interface QMyIncomeVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)QIncomeModel *model;
@property (nonatomic,assign)BOOL isShow;
@property (nonatomic,assign)int tiXianType;
@property (nonatomic,strong)NSMutableArray *cardInfo;
@end

@implementation QMyIncomeVC
-(NSMutableArray *)cardInfo{
    if (!_cardInfo) {
        _cardInfo = [NSMutableArray array];
    }
    return _cardInfo;
}
- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"我的收益";
    self.title = @"";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createUI];
    [self requestData];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ReloadTiXianData) name:@"TiXianActionNotification" object:nil];

}
- (void)ReloadTiXianData{
    
    [self requestData];
}
//获取用户佣金余额
- (void)requestData{
    NSString * url =[QParamService urlWithAction:@"getMyCommission"];
    NSDictionary * para =[QParamService getMyCommission];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error) {
            NSLog(@"-------error:%@--------",error);
            return ;
        }
        QBaseModel * baseModel =[QBaseModel objectFromDict:dict];
        NSLog(@"-----ok--dic:%@",dict);
        if ([baseModel.result isEqualToString:@"success"]) {
            _model = [QIncomeModel objectFromDict:baseModel.items];
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:baseModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    //半透明
    self.navigationController.navigationBar.translucent = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];

}

-(void)createUI
{
    self.tiXianType = 0;
    self.view.clipsToBounds = YES;
    [self.view addSubview:self.tbView];
    
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    self.tbView.scrollEnabled = NO;
    [self.tbView registerNib:[UINib nibWithNibName:@"QIncomeCell" bundle:nil] forCellReuseIdentifier:@"QIncomeCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QLevelCell" bundle:nil] forCellReuseIdentifier:@"QLevelCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QIncomePhoneCell" bundle:nil] forCellReuseIdentifier:@"QIncomePhoneCell"];
    

}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 2||section == 0) {
        return 5;
    }
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.05;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row ==0) {
        return 200;
    }else{
        return 55;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
       
        return 5;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            QIncomeCell * cell =[tableView dequeueReusableCellWithIdentifier:@"QIncomeCell" forIndexPath:indexPath];
            cell.model = _model;
            return cell;

        }else{
            QLevelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QLevelCell" forIndexPath:indexPath];
            if (indexPath.row == 1) {
                cell.titileLab.text = @"累计订单统计";
            }else if (indexPath.row == 2){
                if ([_model.lowLevel isEqualToString:@""]) {
                    cell.titileLab.text = @"我的上级";
                    cell.courtLab.hidden = YES;
                }else{
                    cell.titileLab.text = @"我的下级";
                    cell.courtLab.hidden = NO;
                    if (_model.lowLevel == nil||[_model.lowLevel isEqualToString:@""]) {
                        cell.courtLab.text = @"0个";
                    }else{
                        cell.courtLab.text = [NSString stringWithFormat:@"%@个",_model.lowLevel];
                    }
                }
            }else if (indexPath.row == 3){
                cell.titileLab.text = @"我的客户";
            }
            else if (indexPath.row == 4){
                cell.titileLab.text = @"我要提现";
            }
            return cell;
        }
    }else{
        QIncomePhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QIncomePhoneCell" forIndexPath:indexPath];
        return cell;

    }
}
//- (void)ChooseCardWithType:(int)type{
//    
//    self.tiXianType = type;
//}
//- (void)TiXianDelegateAction
//{
//    
//    
//    BindingCardVC *vc1 = [[BindingCardVC alloc]init];
//    
////    
//    if (self.tiXianType == 0) {
//        NSLog(@"银行卡提现");
//        MyCardVC *vc = [[MyCardVC alloc]init];
//        vc1.title = @"绑定银行卡";
//        vc1.type = 0;
//        [self.navigationController pushViewController:vc1 animated:YES];
//    }
//    if (self.tiXianType == 1) {
//        NSLog(@"支付宝提现");
//        vc1.type = 1;
//        vc1.title = @"绑定支付宝";
//        [self.navigationController pushViewController:vc1 animated:YES];
//    }
//    if (self.tiXianType == 2) {
//        NSLog(@"微信提现");
//         vc1.type = 2;
//        vc1.title = @"绑定微信";
//        [self.navigationController pushViewController:vc1 animated:YES];
//    }
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            //累计订单统计
            QOrderRecordVC * vc =[[QOrderRecordVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 2){
            //我的上级
            QMySuperiorVC * vc =[[QMySuperiorVC alloc]init];
            if ([_model.lowLevel isEqualToString:@""]) {
                vc.title = @"我的上级";
            }else{
                vc.title = @"我的下级";
            }

            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 3){
            //我的客户
            QMyCustomerVC * vc =[[QMyCustomerVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 4){
            //我要提现
            [self RequestIfNoCardData];
            
//            CashVC *vc = [[CashVC alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
//            NSDictionary *dic = [self.cardInfo objectAtIndex:0];
//            vc.cardInfo = dic;

            
        }
    }else if (indexPath.section == 1){
        //打电话
        [QViewUtility showPlayBoxView:PlayboxCallPhoneStyle myBlock:^(NSInteger index) {
            if(index == 1){
                [QViewUtility callPhone];
            }
        }];

    }
}

- (void)RequestIfNoCardData{
    NSString *url = [QParamService urlWithAction:@"userinfo/getUssBank"];
    NSDictionary *para= [QParamService GainIsOrNoBanks];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility disMissNoRecordInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            NSArray *arr = basModel.items;
            self.cardInfo = arr.mutableCopy;
            if (self.cardInfo.count >0) {
                CashVC *vc = [[CashVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [QViewUtility showAlertChooseViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请先绑定提现账户" leftTitle:@"取消" rightTitle:@"确定" type:1 myBlock:^(NSInteger tag) {
                    if(tag == 1){
                        BingdingVC*vc = [[BingdingVC alloc]init];
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }];
            }
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}
#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}


@end
